﻿define(function (require, exports, module) {
    var CReader_TextSelectionToolHandler = require("./TextSelectionToolHandler.js");
    var CTextSelection_DocViewEventHandler = require('./TextSelectionDocViewEventHandler.js');
    var CTextSelection_PageEventHandler = require('./TextSeletionPageEvent.js');
    FXREADER.CTextSelectionPlugin = function (pReaderApp) {
        var _pReaderApp = pReaderApp,
            _bInited = false,
            _Language = _pReaderApp.GetLanguage(),
            _pSelectionToolHandler = null,
             _self = this;

        //获取Plugin名称;
        this.GetPluginName = function () {
            return "TextSelection Plugin";
        };

        //插件被注册的时候调用;
        this.PIOnRegister = function () {
            if (_bInited)
                return true;
        };

        //模块初始化;
        this.PIInit = function () {
            var pTextSelectionPageEventHandler = new CTextSelection_PageEventHandler(_self);
            _pReaderApp.RegisterPageEventHandler(pTextSelectionPageEventHandler);
            _pSelectionToolHandler = new CReader_TextSelectionToolHandler(_pReaderApp);
            var name = _pSelectionToolHandler.Tool_GetName();
            var pTextSelectionDocViewEventHandler = new CTextSelection_DocViewEventHandler(_self);
            _pReaderApp.RegisterDocViewEventHandler(pTextSelectionDocViewEventHandler);
            _pReaderApp.RegisterToolHandler(_pSelectionToolHandler);
            //     _pReaderApp.SetCurToolHandler(_pSelectionToolHandler); //测试用
            this.CreateCmdAndBtn();
            _bInited = true;
        };

        //模块卸载;
        this.PIUnload = function () {
        };

        this.GetReaderApp = function () {
            return _pReaderApp;
        };

        this.CreateCmdAndBtn = function () {
            _pReaderApp.AddCommandUI({
                UIName: "HandTool",
                CommandName: 'HandToolSelect',
                State: false,
                CreateHtmlContentFunc: function () {
                	 return "<a id='btnHandToolSelect' class='fwr_toolbar_btn fwr_toobar_handtool_dis fwr_toolbar_text_btn' title='" + _Language.CommandUI.HandToolTitle + "' cmd='HandToolSelect'>"
                     +"<span class='fwr_img_24by24 fwr_toolbar_img'><img alt='' class='fwr_img_24by24' src='images/hand_select.png' cmd='HandToolSelect'></span><span class='fwr_toollabel' cmd='HandToolSelect'>" + _Language.CommandUI.HandTool + "</span></a>";           
                },
                OnCommandStateChange: function (data) {
                	if(data.state == false)
                	{
                		FXREADER.Common.UpdateBtnState("btnHandToolSelect", "fwr_toobar_handtool_dis", "fwr_toobar_handtool_active", true);
                	}
                	else
                	{
                		FXREADER.Common.UpdateBtnState("btnHandToolSelect", "fwr_toobar_handtool_active", "fwr_toobar_handtool_dis", true);
                	}
                },
                Init: function () {
                	 var pHandToolCommand = _pReaderApp.GetCommand("HandToolSelect");
                     pHandToolCommand && pHandToolCommand.SetState(true);
                }
            });
            _pReaderApp.AddCommand({
                state: true,
                name: "HandToolSelect",
                exec: function (data) {
                    var curTool = _pReaderApp.GetCurToolHandler();
                    var handTool = _pReaderApp.GetToolHandlerByName(FXREADER.TOOL_NAME_HAND);
                    if (handTool != curTool && handTool != null) {
                        _pReaderApp.SetCurToolHandler(handTool);
                       
                    }     

                }
            });
            _pReaderApp.AddCommandUI({
                UIName: "TextTool",
                CommandName: 'TextToolSelect',
                State: false,
                CreateHtmlContentFunc: function () {
                    return "<a id='btnTextToolSelect' class='fwr_toolbar_btn fwr_toobar_texttool_dis fwr_toolbar_text_btn' title='" + _Language.CommandUI.TextSeletionToolTitle + "' cmd='TextToolSelect'>"
                    +"<span class='fwr_img_24by24 fwr_toolbar_img'><img alt='' class='fwr_img_24by24' src='images/text_select.png' cmd='TextToolSelect'></span><span class='fwr_toollabel' cmd='TextToolSelect'>" + _Language.CommandUI.TextSeletionTool + "</span></a>";
                },
                OnCommandStateChange: function (data) {
                	if(data.state == false)
                	{
                		FXREADER.Common.UpdateBtnState("btnTextToolSelect","fwr_toobar_texttool_dis", "fwr_toobar_texttool_active", true);
                	}
                	else
                	{
                		FXREADER.Common.UpdateBtnState("btnTextToolSelect", "fwr_toobar_texttool_active","fwr_toobar_texttool_dis", true);
                	}
                },
                Init: function () {
                    
                }
            });
            _pReaderApp.AddCommand({
                state: true,
                name: "TextToolSelect",
                exec: function (data) {
                    var curTool = _pReaderApp.GetCurToolHandler();
                    var textTool = _pReaderApp.GetToolHandlerByName(FXREADER.TOOL_NAME_SELECTTEXT);
                    if (textTool != curTool && textTool != null) {
                        _pReaderApp.SetCurToolHandler(textTool);
                       
                    }
                }
            });
        };

        this.OnDocViewZoom = function (scale) {
            var textSelectTool = _pSelectionToolHandler.GetTextSelectTool();
            if (textSelectTool != null) {
                textSelectTool.HighLightRects();
                textSelectTool.HideSelectionOperationDiv();
            }

        };

        this.OnPreDocViewResize = function () {
            var textSelectTool = _pSelectionToolHandler.GetTextSelectTool();
            if (textSelectTool != null) {
                textSelectTool.HideSelectionOperationDiv();
                textSelectTool.HideHighLightRects();
            }
            
        };

        this.OnPageShowComplete = function (pPageView) {
            if (pPageView != null) {
                var textManager = _pReaderApp.GetTextManager();
                if (textManager != null) {
                	textManager.AjaxGetTextPage(pPageView.GetPageIndex(),true);
                }
            }
        };

    };

    exports.Reader_CreatePlugin = function (pReaderApp) {
        var pTextSelectionPlugin = new FXREADER.CTextSelectionPlugin(pReaderApp);
        return pTextSelectionPlugin;
    };
});